/**
 * Created by weidong on 14-2-17.
 */
var FaultFlag = 1;
var SuccessFlag =0;
var url = require('url');
var log = require('../lib/log');
var dbvirapinfo = require('../models/virapinfo');
var jsondata=require("../models/jsondata");
function apauthservlet(request,response)
{
    if(request.method.toUpperCase()==="POST")
    {
        //ap和adwifi注册上线的交互
        log.info("[APAUTHSERVLET]: receive a post request for adwifi/apauthservlet.url parameter is "+url.parse(request.url).query);
        //parse request json data
        //var apmac = request.body.apCom.apComHead.apMac;
       // var apmac='aa:bb:cc:dd';
        var apmac = request.body.apmac;
        var apComRetHead ='';
        var responejsondata = '';

        console.log("request apmac is "+apmac);

        dbvirapinfo.selectMac(apmac,function(error,results){
            if((error)!== null)
            {
                // 数据库异常
                apComRetHead = jsondata.constract_ap_com_ret_head(FaultFlag,error.errno);
                responejsondata = "{\n\t apComRet:{\n\t\tapComRetHead:"+apComRetHead+"\n\t}\n}";
            }
            else if(results.length <= 0) {
                // 不存在，直接响应页面
                apComRetHead = jsondata.constract_ap_com_ret_head(FaultFlag,error.errno);
                responejsondata = "{\n\t apComRet:{\n\t\tapComRetHead:"+apComRetHead+"\n\t}\n}";
            } else {
                apComRetHead = jsondata.constract_ap_com_ret_head(SuccessFlag,'');
                responejsondata = "{\n\t apComRet:{\n\t\tapComRetHead:"+apComRetHead+"\n\t}\n}";
            }

            response.send(responejsondata);
            response.end();
        });
    }
    else if(request.method.toUpperCase()==="GET")
    {
        //STA上线交互流程中的请求
        log.info("[APAUTHSERVLET]: receive a get request for adwifi/apauthservlet.url parameter is "+url.parse(request.url).query);
        //根据action参数进行相应处理
         if(request.query.action ==="topage")
         {
             console.log("[APAUTHSERVLET]: redirect sta to first rul page")
             response.redirect(request.query.wlanuserfirsturl);
         }
        else
         {
             response.redirect(request.query.wlanuserfirsturl);
         }

    }



}

exports.apauthservlet = apauthservlet;